package com.biboheart.demo.report.domain.impl;

import com.biboheart.breport.core.BrHandler;
import com.biboheart.breport.dataset.BrReport;
import com.biboheart.breport.source.BrDataSource;
import com.biboheart.breport.template.BrTemplate;
import com.biboheart.brick.utils.BeanUtils;
import com.biboheart.brick.utils.JsonUtils;
import com.biboheart.brick.utils.TimeUtils;
import com.biboheart.demo.report.domain.ReportDomain;
import com.biboheart.demo.report.repository.entity.SettleOrderItems;
import com.biboheart.demo.report.repository.service.SettleOrderItemsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
@RequiredArgsConstructor
public class ReportDomainImpl implements ReportDomain {
    private final SettleOrderItemsService settleOrderItemsService;

    @Override
    public BrReport createReport() {
        BrDataSource dataSource = this::getDataList;
        BrTemplate template = this::getReportTemplate;
        BrHandler brHandler = new BrHandler(dataSource, template);
        return brHandler.run();
    }

    private List<Map<String, Object>> getDataList() {
        List<SettleOrderItems> orderItemsList = settleOrderItemsService.list(TimeUtils.getMonthsFirstDayZeroMillos(0, -1), TimeUtils.getMonthsFirstDayZeroMillos(0, 0));
        List<Map<String, Object>> mapList = new ArrayList<>();
        int i = 0;
        for (SettleOrderItems orderItems : orderItemsList) {
            Map<String, Object> map = new HashMap<>();
            BeanUtils.beanToMap(orderItems, map, null, null);
            mapList.add(map);
            if (i ++ >= 100) {
                break;
            }
        }
        return mapList;
    }

    private String getReportTemplate() {
        return "{\n" +
                "    \"cols\": [{\n" +
                "            \"sn\": \"sn\",\n" +
                "            \"name\": \"源编号\",\n" +
                "            \"width\": 0,\n" +
                "            \"align\": \"left\",\n" +
                "            \"expression\": \"sourceId\"\n" +
                "        }, {\n" +
                "            \"sn\": \"projectSn\",\n" +
                "            \"name\": \"项目编号\",\n" +
                "            \"width\": 0,\n" +
                "            \"type\": 16,\n" +
                "            \"align\": \"left\"\n" +
                "        }, {\n" +
                "            \"sn\": \"projectName\",\n" +
                "            \"name\": \"项目名称\",\n" +
                "            \"width\": 0,\n" +
                "            \"align\": \"left\"\n" +
                "        }, {\n" +
                "            \"sn\": \"num\",\n" +
                "            \"name\": \"数量\",\n" +
                "            \"width\": 0,\n" +
                "            \"type\": 8,\n" +
                "            \"align\": \"left\"\n" +
                "        }, {\n" +
                "            \"sn\": \"saleUnit\",\n" +
                "            \"name\": \"单位\",\n" +
                "            \"width\": 0,\n" +
                "            \"align\": \"left\"\n" +
                "        }, {\n" +
                "            \"sn\": \"price\",\n" +
                "            \"name\": \"单价\",\n" +
                "            \"width\": 0,\n" +
                "            \"align\": \"left\"\n" +
                "        }, {\n" +
                "            \"sn\": \"payment\",\n" +
                "            \"name\": \"金额\",\n" +
                "            \"width\": 0,\n" +
                "            \"type\": 8,\n" +
                "            \"align\": \"left\"\n" +
                "        }, {\n" +
                "            \"sn\": \"chargeTypeName\",\n" +
                "            \"name\": \"费别\",\n" +
                "            \"width\": 0,\n" +
                "            \"align\": \"left\"\n" +
                "        }, {\n" +
                "            \"sn\": \"departmentName\",\n" +
                "            \"name\": \"就诊科室\",\n" +
                "            \"width\": 0,\n" +
                "            \"align\": \"left\"\n" +
                "        }, {\n" +
                "            \"sn\": \"billPersonName\",\n" +
                "            \"name\": \"开单医生\",\n" +
                "            \"width\": 0,\n" +
                "            \"align\": \"left\"\n" +
                "        }, {\n" +
                "            \"sn\": \"billTime\",\n" +
                "            \"name\": \"开单时间\",\n" +
                "            \"width\": 0,\n" +
                "            \"align\": \"left\"\n" +
                "        }, {\n" +
                "            \"sn\": \"endTime\",\n" +
                "            \"name\": \"结算时间\",\n" +
                "            \"width\": 0,\n" +
                "            \"align\": \"left\"\n" +
                "        }, {\n" +
                "            \"sn\": \"patientName\",\n" +
                "            \"name\": \"患者姓名\",\n" +
                "            \"width\": 0,\n" +
                "            \"align\": \"left\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
    }
}
